package com.hua.nailcampus.acl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hua.nailcampus.acl.mapper.*;
import com.hua.nailcampus.acl.model.*;
import com.hua.nailcampus.acl.model.vo.DynamicLikesVo;
import com.hua.nailcampus.acl.model.vo.DynamicReplyVo;
import com.hua.nailcampus.acl.model.vo.DynamicVo;
import com.hua.nailcampus.acl.service.DynamicService;
import com.hua.nailcampus.constant.RedisConst;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author:chenjianhua
 * @Date:2023/12/21 20:50
 * @Version 17
 */
@Slf4j
@Service
public class DynamicServiceImpl extends ServiceImpl<DynamicMapper, Dynamic>
    implements DynamicService {

    @Autowired
    private DynamicMapper dynamicMapper;

    @Autowired
    private ResourcesMapper resourcesMapper;

    @Autowired
    private FriendsMapper friendsMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AvatarMapper avatarMapper;

    @Autowired
    private DynamicReplyMapper dynamicReplyMapper;

    @Autowired
    private DynamicLikesMapper dynamicLikesMapper;

    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public void publish(DynamicVo dynamicVo) {
//        创建Dynamic对象
        Dynamic dynamic = new Dynamic(dynamicVo);
//        存入数据库
        dynamicMapper.insert(dynamic);
        dynamicVo.setUserName(userMapper.selectById(dynamicVo.getUserId()).getUserName());
        List<DynamicLikesVo> likesList = new ArrayList<>();
        dynamicVo.setLikesList(likesList);
        List<DynamicReplyVo> replyList = new ArrayList<>();
        dynamicVo.setReplyList(replyList);
        Avatar avatar = avatarMapper.selectOne(new LambdaQueryWrapper<Avatar>().eq(Avatar::getUserId, dynamicVo.getUserId()));
        if(avatar!=null){
            dynamicVo.setUserAvatar(avatar.getUrl());
        }
//        获取存入数据库中Dynamic对象的ID
        List<Dynamic> dynamicList = dynamicMapper.selectList(new LambdaQueryWrapper<Dynamic>()
                .eq(Dynamic::getUserId, dynamicVo.getUserId()));
        Dynamic newDynamic = dynamicList.get(dynamicList.size()-1);
//        将该动态的所有图片存入数据库
        for (String url:dynamicVo.getResourceList()){
            Resources resources = new Resources();
            resources.setUrl(url);
            resources.setDynamicOrReplyId(newDynamic.getId());
            resourcesMapper.insert(resources);
        }
//        更新Redis中的数据
        List<DynamicVo> dynamicVoList = (List<DynamicVo>) redisTemplate.opsForValue().get(RedisConst.DYNAMIC_LISTK+dynamicVo.getUserId().toString());
        Collections.reverse(dynamicVoList);
        dynamicVoList.add(dynamicVo);
        Collections.reverse(dynamicVoList);
        redisTemplate.opsForValue().set(RedisConst.DYNAMIC_LISTK+dynamicVo.getUserId(),dynamicVoList,RedisConst.DYNAMIC_LIST_TIMEOUT,TimeUnit.MINUTES);
    }

    /**
     * 获取所有动态
     * @param userId
     * @return
     */
    @Override
    public List<DynamicVo> getAllDynamic(Integer userId) {
//        获取用户ID所有好友
        List<Friends> friendsList = friendsMapper.selectList(new LambdaQueryWrapper<Friends>()
                .eq(Friends::getUId, userId));
//        创建DynamicVoList
        List<DynamicVo> dynamicVoList = new ArrayList<>();
//        根据好友ID查询好友发过的所有动态
        for (Friends friend : friendsList){
//            获取好友用户信息
            User user = userMapper.selectById(friend.getFId());
            List<Dynamic> dynamicList = dynamicMapper.selectList(new LambdaQueryWrapper<Dynamic>()
                    .eq(Dynamic::getUserId, user.getId()));
            DynamicVo dynamicVo = null;
            for (Dynamic dynamic : dynamicList){
//                创建动态数据传输对象
                dynamicVo = new DynamicVo(dynamic);
//                根据动态ID获取动态的图片或者视频
                dynamicVo.setResourceList(resourcesMapper.selectFileList(dynamic.getId()));
//                将好友信息放入动态数据传输对象中
//                判断是否为好友设置了昵称
                if(friend.getFName()!=null){
//                    若有昵称则设置昵称
                    dynamicVo.setUserName(friend.getFName());
                }else{
//                    没有昵称则设置用户名
                    dynamicVo.setUserName(user.getUserName());
                }
//                获取好友用户头像
                Avatar avatar = avatarMapper.selectOne(new LambdaQueryWrapper<Avatar>()
                        .eq(Avatar::getUserId, friend.getFId()));
//                判断是否有头像,有头像则使用头像
                if(avatar!=null){
                    dynamicVo.setUserAvatar(avatar.getUrl());
                }
                if(dynamic!=null){
                    dynamicVoList.add(dynamicVo);
                }
//               获取动态回复列表
                List<DynamicReplyVo> dynamicReplyVoList =  dynamicReplyMapper.selectDynamicReplyVoList(dynamic.getId());
//                将动态回复列表放入动态数据传输对象中
                dynamicVo.setReplyList(dynamicReplyVoList);
//                获取点赞列表
                List<DynamicLikesVo> dynamicLikesVoList = dynamicLikesMapper.selectDynamicLikesVoList(dynamic.getId());
                dynamicVo.setLikesList(dynamicLikesVoList);
            }
        }
//        将动态列表中的动态根据时间来进行排序
        if (dynamicVoList.size()>1){
            dynamicVoList.sort(Comparator.comparing(DynamicVo::getCreateTime));;
            Collections.reverse(dynamicVoList);
        }
//        将数据存入Redis中
        redisTemplate.opsForValue().set(RedisConst.DYNAMIC_LISTK+userId.toString(),dynamicVoList,RedisConst.DYNAMIC_LIST_TIMEOUT, TimeUnit.MINUTES);
        return dynamicVoList;
    }
}
